package com.kingint.web.portal;

import com.kingint.mbg.ser.mapper.SerDepartmentRepairMapper;
import com.kingint.mbg.sys.entity.UmsMember;
import com.kingint.web.admin.base.service.BasServiceClassService;
import com.kingint.web.portal.serve.service.SendMessageService;
import com.kingint.web.portal.serve.service.SerWorkOrderService;
import com.kingint.web.sys.service.NoticeService;
import com.kingint.web.sys.service.UmsAdminService;
import com.kingint.web.sys.service.UmsMemberService;
import com.kingint.web.vo.*;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RunWith(SpringRunner.class)
@SpringBootTest
public class test {
    @Autowired
    private RepositoryService repositoryService;//提供一系列管理流程部署和流程定义的API
    @Autowired
    private RuntimeService runtimeService;//在流程运行时对流程实例进行管理与控制
    @Autowired
    private TaskService taskService;//对流程任务进行管理，例如任务提醒、任务完成和创建任务等
    @Autowired
    private HistoryService historyService;//对流程的历史数据进行操作，包括查询、删除这些历史数据

    @Resource
    private SerDepartmentRepairMapper departmentRepairMapper;
    @Resource
    private RepairBean repairBean;
    @Resource
    private MessageBean messageBean;
    @Resource
    private SendMessageService sendMessageService;
    @Resource
    private RushBean rushBean;
    @Resource
    private BasServiceClassService serviceClassService;
    @Autowired
    public SerWorkOrderService serWorkOrderService;
    @Resource
    private UmsAdminService adminService;
    @Resource
    private ServiceOrderBean serviceOrderBean;
    @Resource
    private UmsMemberService memberService;



    @Autowired
    public NoticeService noticeService;

    @Test
    public void ceshiDepartment() {
        UmsMember userInfo = memberService.getUserInfo();
        System.out.println(userInfo);
    }


    @Test
    public void deploy() {//部署流程
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("processes/roomclear.bpmn")
                .addClasspathResource("processes/roomclear.png")
                .name("客房清洁工单")
                .deploy();
        System.out.println("流程部署版本：" + deploy.getVersion());
        System.out.println("流程部署名称:" + deploy.getName());
        System.out.println("流程部署id:" + deploy.getId());
    }

    @Test
    public void start() {//启动流程
        String processDefinitionKey =serviceOrderBean.getProcessInstanceKey();//每一个流程有对应的一个key这个是某一个流程内固定的写在bpmn内的
        Map<String, Object> map = new HashMap<>();
        map.put(serviceOrderBean.getCreateNameEl(), "。。。华*");
        String businessKey = "warrantyBillOrder_" + "0_2";
        ProcessInstance instance = runtimeService
                .startProcessInstanceByKey(processDefinitionKey, businessKey, map);
        System.out.println("流程实例名称：" + instance.getName());
        System.out.println("流程关联的数据：" + instance.getBusinessKey());
        System.out.println("流程实例ID:" + instance.getId());
        System.out.println("流程定义ID:" + instance.getProcessDefinitionId());
    }

    /**
     * 查询当前人的个人任务
     */
    @Test
    public void findTask() {
        List<Task> list = taskService.createTaskQuery()//创建任务查询对象
                .processInstanceId("5253d073-5abf-11eb-9b24-1a1deae96ccc")
                .list();
        System.out.println(list);
        if (list != null && list.size() > 0) {
            for (Task task : list) {
                System.out.println("任务ID:" + task.getId());
                System.out.println("任务名称:" + task.getName());
                System.out.println("任务的创建时间:" + task.getCreateTime());
                System.out.println("任务的办理人:" + task.getAssignee());
                System.out.println("流程实例ID：" + task.getProcessInstanceId());
                System.out.println("执行对象ID:" + task.getExecutionId());
                System.out.println("流程定义ID:" + task.getProcessDefinitionId());
                System.out.println("挂起状态：" + task.isSuspended());
                Map<String, Object> processVariables = task.getProcessVariables();
                System.out.println(processVariables);

                HashMap<String, Object> map = new HashMap<>();
                map.put(serviceOrderBean.getCompleteStatus(), 2);//挂起此单
                //同时设置处理人
                map.put(serviceOrderBean.getHandleUserEl(), "。。。华*");
                taskService.setVariables(task.getId(),map);
                taskService.complete(task.getId());

//                HashMap<String, Object> map = new HashMap<>();
//                map.put(serviceOrderBean.getHandleUserEl(),"。。。华*");
//                taskService.complete(task.getId(),map);
//                taskService.complete(task.getId(),variables);
//                HashMap<String, Object> map = new HashMap<>();
//                map.put(serviceOrderBean.getHandleUserEl(), "。。。华*");//下一阶段处理人
//                map.put(serviceOrderBean.getCompleteStatus(), 1);
//                taskService.setVariables(task.getId(),map);
//                taskService.complete(task.getId());
//                map.put(repairBean.getReceiveStatus(),1);
//                map.put(repairBean.getCompleteStatus(),-1);

//                taskService.complete(task.getId(),map);
//                taskService.complete(task.getId(), map);
//                taskService.setAssignee(task.getId(),"。。。华*");
//                taskService.complete(task.getId());
//                HashMap<String, Object> map = new HashMap<>();
//                map.put(rushBean.getCheckNameEl(), "。。。华*");
//                map.put("rejectStatus", 0);//完成状态
//                map.put("completeStatus", 1);//完成状态
//                map.put("checkUser", "only girl-");//下一阶段处理人
//                taskService.setVariables(task.getId(), map);
//                taskService.complete(task.getId());
//                map.put("repairUser", "only girl-");//下一阶段处理人
//                map.put("receiveStatus", 1);//确定状态
//                map.put("repairUser", "。。。华*");//下一阶段处理人
//                taskService.setVariables(task.getId(), map);
//                taskService.complete(task.getId());
            }
        }
    }

    @Test
    public void cancelTask() {//完成任务
        HashMap<String, Object> map = new HashMap<>();
        map.put(repairBean.getReceiveStatus(), 1);//确定状态
        map.put(repairBean.getReceiveNameEl(), "。。。华*");//下一阶段处理人
        //任务设置流程变量
        taskService.setVariables("8d53a034-49b8-11eb-bb92-1a1deae96ccc", map);//在任务中设置流程变量
        taskService.complete("8d53a034-49b8-11eb-bb92-1a1deae96ccc");
    }

    //暂停与激活流程实例
    @Test
    public void activitieProcessInstance() {
        runtimeService.suspendProcessInstanceById("93d5b1a9-45b3-11eb-8fe6-1a1deae96ccc");
        System.out.println("挂起流程实例");
//        runtimeService.activateProcessInstanceById("9712ff48-3b57-11eb-85cc-1a1deae96ccc");
//        System.out.println("激活流程实例");
    }


    /**
     * 查询历史流程实例
     */
    @Test
    public void queryHistoricInstance() {
        List<HistoricProcessInstance> list = historyService
                .createHistoricProcessInstanceQuery()
                .processDefinitionKey("warrantyBillOrder")
                .orderByProcessInstanceStartTime().asc()//排序
                .list();
        if (list != null && list.size() > 0) {
            for (HistoricProcessInstance hpi : list) {
                System.out.println("流程启动添加的参数：" + hpi.getBusinessKey());
                System.out.println("流程定义ID：" + hpi.getProcessDefinitionId());
                System.out.println("流程实例ID：" + hpi.getId());
                System.out.println("开始时间：" + hpi.getStartTime());
                System.out.println("结束时间：" + hpi.getEndTime());
                System.out.println("流程持续时间：" + hpi.getDurationInMillis());
                System.out.println("=======================================");
            }
        }
    }

    /**
     * 某一次流程执行了多少步
     */
    @Test
    public void queryHistoricActivitiInstance() {
        String processInstanceId = "c0ca46b9-464d-11eb-b2ea-1a1deae96ccc";
        List<HistoricActivityInstance> list = historyService
                .createHistoricActivityInstanceQuery()
                .processInstanceId(processInstanceId)
                .orderByHistoricActivityInstanceStartTime()
                .desc()
                .list();
        if (list != null && list.size() > 0) {
            for (HistoricActivityInstance hai : list) {
                System.out.println(hai.getId());
                System.out.println("步骤ID：" + hai.getActivityId());
                System.out.println("步骤名称：" + hai.getActivityName());
                System.out.println("执行人：" + hai.getAssignee());
                System.out.println("开始时间：" + hai.getStartTime());
                System.out.println("====================================");
            }
        }
    }

    /**
     * 某一次流程的执行经历的多少任务
     */
    @Test
    public void queryHistoricTask() {
//        String processInstanceId = "afae680b-4100-11eb-bd6b-1a1deae96ccc";
        List<HistoricTaskInstance> list = historyService
                .createHistoricTaskInstanceQuery()
                .processDefinitionKey("warrantyBillOrder")
                .taskName("创建维修单")
                .taskAssignee("only girl-")
                .processInstanceBusinessKeyLikeIgnoreCase("%1_3%")
                .unfinished()
                .list();
        if (list != null && list.size() > 0) {
            for (HistoricTaskInstance hti : list) {
                String executionId = hti.getExecutionId();
                System.out.println("processInstanceId:" + hti.getProcessInstanceId());
                System.out.println("executionId:" + executionId);
                System.out.println("taskId:" + hti.getId() + "，");
                System.out.println("name:" + hti.getName() + "，");
                System.out.println("pdId:" + hti.getProcessDefinitionId() + "，");
                System.out.println("assignee:" + hti.getAssignee() + "，");
                System.out.println("===============================");
            }
        }
    }

    /**
     * 某一次流程的执行时设置的流程变量
     */
    @Test
    public void queryHistoricVariables() {
        String processInstanceId = "561e4caf-44f3-11eb-9ef4-1a1deae96ccc";
        List<HistoricVariableInstance> list = historyService
                .createHistoricVariableInstanceQuery()
                .processInstanceId(processInstanceId)
                .list();
        if (list != null && list.size() > 0) {
            for (HistoricVariableInstance hvi : list) {
                System.out.println("piId:" + hvi.getProcessInstanceId());
                System.out.println("variablesName:" + hvi.getVariableName());
                System.out.println("variablesValue:" + hvi.getValue());
            }
        }
    }

    /**
     * 删除流程实例
     */
    @Test
    public void deleteProcessInfo() {
        runtimeService.deleteProcessInstance("1ba5dbc3-5322-11eb-9c98-002596021000", "删除测试");
    }
    @Test
    public void test0() throws Exception {
        System.out.println("main函数开始执行");
        List<Integer> list=new ArrayList<>();
        list.add(1);
         new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("===task start===");
                System.out.println(list.get(0));
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("===task finish===");
            }
        }).start();
        System.out.println("main函数执行结束");

    }
}

